package domain;

import org.hibernate.Session;

import util.HibernateUtil;

public class DAOPhoneNumber {
	
	Session session = null;
	private static DAOPhoneNumber dao=null;
	
	public DAOPhoneNumber(){		
		session = HibernateUtil.getSessionFactory().openSession();
	}
	
	
	public static DAOPhoneNumber getInstance(){
		if(dao==null)
			dao=new DAOPhoneNumber();
		return dao;
	}
	
	
	public PhoneNumber addPhoneNumber(String phoneKind,String phoneNumber){
		try{
			//begin a transaction
			org.hibernate.Transaction tx = session.beginTransaction();
			//create a contact and save it into the DB
			PhoneNumber ph=new PhoneNumber();
			ph.setPhoneKind(phoneKind);
			ph.setPhoneNumber(phoneNumber);
			//save the contact into the DB
			session.save(ph); // or session.persist(contact);
			//mandatory to flush the data into the DB
			tx.commit();
			return ph;
			}catch(Exception e){
						System.out.println(e.getMessage());
			}
			return null;
	}
	
	public boolean modifyAdress(long id, String phoneNumber){
		boolean success = false;
		PhoneNumber ph=null;
		
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			ph=(PhoneNumber) session.get(PhoneNumber.class, id);
			ph.setPhoneNumber(phoneNumber);
			
			success = true;
			tx.commit();
		} catch( Exception e ){
			e.printStackTrace();
		}
		return success;		
	}
}
